import datetime
import requests
import os
import json


class WxRobot:
    def __init__(self):
        # print(3)
        self.wx = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=155a7793-16b0-4a35-8f54-b21026efb945'
        self.error = self.get_allure_error()
        # (self.get_allure_error())111111111111111111111
        # print(1)

    def get_allure_error(self):
        d = {}
        # 获取项目绝对路径
        path = os.path.abspath(os.path.dirname((__file__)))
        # 打开prometheusData 获取需要发送的信息2222222222222

        json_file_path = os.path.join(os.getcwd(), "allure-report/widgets/suites.json")
        print(json_file_path)
        # 检查文件是否存在
        if not os.path.isfile(json_file_path):
            print("文件不存在")
            print(json_file_path)
            exit()

        # 打开文件并读取内容
        with open(json_file_path, 'r') as file:
            jenkins_data= json.load(file)

        # 打印读取的数据
        print(jenkins_data)
        case_error = jenkins_data["items"][0]["statistic"]["failed"] + jenkins_data["items"][0]["statistic"]["broken"]
        return case_error

    def send_report(self):
        if self.error > 0:
            headers = {"Content-Type": "application/json;charset=utf-8"}
            body = {
                "msgtype": "text",
                "text": {
                    "content": datetime.datetime.now().strftime(
                        '%Y-%m-%d %H:%M:%S') + "   接口自动化框架运行失败\n" + "http:///root:Aa123456!@47.97.38.21:8080/job/abc/allure",
                    "mentioned_mobile_list": ["13898379211"]
                }
            }
            response = requests.post(self.wx, headers=headers, json=body)
            print(response)

        else:
            print('无报错')
        # return 2

if __name__ == '__main__':
    d = WxRobot()
    print(d.send_report())

